package com.sushi.service.impl;

import com.sushi.repository.entity.CommodityEntity;
import com.sushi.repository.mapper.CommodityMapper;
import com.sushi.request.TagRelationReq;
import com.sushi.service.CommodityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sushi.response.ResponseBO;
import com.sushi.response.ResponsePageBO;
import com.sushi.response.CommodityResp;
import com.sushi.request.CommodityReq;
import com.sushi.request.CommodityPageReq;
import com.sushi.service.TagRelationService;
import com.sushi.utils.*;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sushi.exception.BusinessException;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品 服务实现类
 * @date 2023-05-02
 * @time 17:13:55
 * @author Administrator
 */
@Slf4j
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, CommodityEntity> implements CommodityService {

	@Autowired
	private CommodityMapper commodityMapper;

    @Autowired
    private TagRelationService tagRelationService;

    

    /**
     * 新增
     */
    @Override
    public ResponseBO<CommodityResp> add(CommodityReq req) {
        CommodityEntity entity = BeanUtil.convert(req, CommodityEntity.class);
        if (ObjectUtil.isEmpty(entity)) {
            throw new BusinessException("保存失败");
        }

//        if (CollectionUtil.isNotEmpty(req.getTypeList())) {
//            TagRelationReq relationReq = new TagRelationReq();
//        }
//
        entity.insert();
        return ResponseUtil.ok(BeanUtil.convert(entity, CommodityResp.class));
    }

    /**
     * 逻辑删除
     */
    @Override
    public ResponseBO<CommodityResp> logicRemove(CommodityReq req) {
        if (StringUtil.isBlank(req.getId())) {
            throw new BusinessException("id不能为空");
        }
        CommodityEntity entity = new CommodityEntity();
        entity.setId(req.getId());
        entity.setDeleted(1L);
        entity.updateById();
        return ResponseUtil.ok(BeanUtil.convert(entity, CommodityResp.class));
    }

    /**
     * 更新
     */
    @Override
    public ResponseBO<CommodityResp> updateByField(CommodityReq req) {
        if (ObjectUtil.isEmpty(req) || StringUtil.isBlank(req.getId())) {
            throw new BusinessException("必要参数不能为空");
        }
        CommodityEntity entity = getById(req.getId());
        if (ObjectUtil.isEmpty(entity)) {
            return ResponseUtil.error("信息不存在，无法更新");
        }
        if (StringUtil.isNotBlank(req.getId())) {
            entity.setId(req.getId());
        }
        if (StringUtil.isNotBlank(req.getName())) {
            entity.setName(req.getName());
        }
        if (ObjectUtil.isNotEmpty(req.getPrice())) {
            entity.setPrice(req.getPrice());
        }
        if (StringUtil.isNotBlank(req.getType())) {
            entity.setType(req.getType());
        }
        if (StringUtil.isNotBlank(req.getUserId())) {
            entity.setUserId(req.getUserId());
        }
        if (StringUtil.isNotBlank(req.getContent())) {
            entity.setContent(req.getContent());
        }
        if (StringUtil.isNotBlank(req.getHeadIconUrl())) {
            entity.setHeadIconUrl(req.getHeadIconUrl());
        }
        if (StringUtil.isNotBlank(req.getPictureUrls())) {
            entity.setPictureUrls(req.getPictureUrls());
        }
        if (ObjectUtil.isNotEmpty(req.getRemainNum())) {
            entity.setRemainNum(req.getRemainNum());
        }
        if (StringUtil.isNotBlank(req.getStatus())) {
            entity.setStatus(req.getStatus());
        }
        if (StringUtil.isNotBlank(req.getTradeType())) {
            entity.setTradeType(req.getTradeType());
        }

        entity.updateById();
        log.info("更新成功：{}", JSON.toJSONString(entity));
        return ResponseUtil.ok(BeanUtil.convert(entity, CommodityResp.class));
    }

    /**
     * 列表
     */
    @Override
    public ResponsePageBO<CommodityResp> listByCondition(CommodityPageReq req) {
        if (ObjectUtil.isEmpty(req) || ObjectUtil.isEmpty(req.getPageNum()) || ObjectUtil.isEmpty(req.getPageSize())) {
            throw new BusinessException("必要参数不能为空");
        }
        LambdaQueryWrapper<CommodityEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommodityEntity::getDeleted, 0L);
        wrapper.eq(StringUtil.isNotBlank(req.getId()), CommodityEntity::getId, req.getId());
        wrapper.eq(StringUtil.isNotBlank(req.getName()), CommodityEntity::getName, req.getName());
        wrapper.eq(ObjectUtil.isNotEmpty(req.getPrice()), CommodityEntity::getPrice, req.getPrice());
        wrapper.eq(StringUtil.isNotBlank(req.getType()), CommodityEntity::getType, req.getType());
        wrapper.eq(StringUtil.isNotBlank(req.getUserId()), CommodityEntity::getUserId, req.getUserId());
        wrapper.eq(StringUtil.isNotBlank(req.getContent()), CommodityEntity::getContent, req.getContent());
        wrapper.eq(StringUtil.isNotBlank(req.getHeadIconUrl()), CommodityEntity::getHeadIconUrl, req.getHeadIconUrl());
        wrapper.eq(StringUtil.isNotBlank(req.getPictureUrls()), CommodityEntity::getPictureUrls, req.getPictureUrls());
        wrapper.eq(ObjectUtil.isNotEmpty(req.getRemainNum()), CommodityEntity::getRemainNum, req.getRemainNum());
        wrapper.eq(StringUtil.isNotBlank(req.getStatus()), CommodityEntity::getStatus, req.getStatus());
        wrapper.eq(StringUtil.isNotBlank(req.getTradeType()), CommodityEntity::getTradeType, req.getTradeType());

        String searchContent = req.getSearchContent();
        if (StringUtil.isNotBlank(searchContent)) {
            wrapper.and(w -> w.or().like(CommodityEntity::getName, searchContent).or().like(CommodityEntity::getContent, searchContent).or()
                    .like(CommodityEntity::getCreatorName, searchContent));
        }

        wrapper.orderByDesc(CommodityEntity::getGmtCreateTime);

        Page<CommodityEntity> page = page(new Page<>(req.getPageNum(), req.getPageSize()), wrapper);
        if (ObjectUtil.isEmpty(page) || CollectionUtil.isEmpty(page.getRecords())) {
            return ResponsePageUtil.ok(req, new ArrayList<>(), 0L);
        }
        List<CommodityResp> convert = BeanUtil.convert(page.getRecords(), CommodityResp.class);
        return ResponsePageUtil.ok(req, convert, page.getTotal());
    }

    /**
     * 详情
     */
    @Override
    public ResponseBO<CommodityResp> detail(CommodityReq req) {
        if (ObjectUtil.isEmpty(req) || StringUtil.isBlank(req.getId())) {
            throw new BusinessException("必要参数不能为空");
        }
        CommodityEntity entity = getById(req.getId());
        if (ObjectUtil.isEmpty(entity)) {
            throw new BusinessException("对象不存在");
        }
        return ResponseUtil.ok(BeanUtil.convert(entity, CommodityResp.class));
    }

}